package com.gwnet.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwnet.rider.service.PlatformWalletLogService;
import com.gwnet.shop.domain.OrderDelivery;
import com.gwnet.shop.enums.OrderConstants;
import com.gwnet.shop.enums.OrderStatusEnum;
import com.gwnet.shop.mapper.OrderDeliveryMapper;
import com.gwnet.shop.mapper.OrderInfoMapper;
import com.gwnet.shop.service.IOrderDeliveryService;
import com.gwnet.shop.service.IOrderInfoService;
import com.gwnet.shop.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 订单发货Service业务层处理
 *
 * @author 张万鹏
 * @date 2024-07-21
 */
@Service
public class OrderDeliveryServiceImpl extends ServiceImpl<OrderDeliveryMapper,OrderDelivery> implements IOrderDeliveryService
{
    @Resource
    private OrderDeliveryMapper orderDeliveryMapper;

    @Resource
    private IOrderInfoService orderInfoService;

    @Resource
    private PlatformWalletLogService walletLogService;


    /**
     * 查询订单发货
     *
     * @param id 订单发货主键
     * @return 订单发货
     */
    @Override
    public OrderDelivery selectOrderDeliveryById(String id)
    {
        return orderDeliveryMapper.selectOrderDeliveryById(id);
    }

    /**
     * 查询订单发货列表
     *
     * @param orderDelivery 订单发货
     * @return 订单发货
     */
    @Override
    public List<OrderDelivery> selectOrderDeliveryList(OrderDelivery orderDelivery)
    {
        return orderDeliveryMapper.selectOrderDeliveryList(orderDelivery);
    }

    /**
     * 新增订单发货
     *
     * @param orderDelivery 订单发货
     * @return 结果
     */
    @Override
    public int insertOrderDelivery(OrderDelivery orderDelivery)
    {
        return orderDeliveryMapper.insertOrderDelivery(orderDelivery);
    }

    /**
     * 修改订单发货
     *
     * @param orderDelivery 订单发货
     * @return 结果
     */
    @Override
    public int updateOrderDelivery(OrderDelivery orderDelivery)
    {
        return orderDeliveryMapper.updateOrderDelivery(orderDelivery);
    }

    /**
     * 批量删除订单发货
     *
     * @param ids 需要删除的订单发货主键
     * @return 结果
     */
    @Override
    public int deleteOrderDeliveryByIds(String[] ids)
    {
        return orderDeliveryMapper.deleteOrderDeliveryByIds(ids);
    }

    /**
     * 删除订单发货信息
     *
     * @param id 订单发货主键
     * @return 结果
     */
    @Override
    public int deleteOrderDeliveryById(String id)
    {
        return orderDeliveryMapper.deleteOrderDeliveryById(id);
    }

    /**
     * 配送状态修改 同时修改orderDelivery 和 orderInfo
     * @param delivery
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int changeStatusAndOrderIfoStatus(OrderDelivery delivery) {

        if(StringUtils.isNotEmpty(delivery.getDeliveryStatus()) && Integer.parseInt(delivery.getDeliveryStatus())<= Integer.parseInt(OrderStatusEnum.SUCCESS.code())){
            OrderDelivery orderDelivery = orderDeliveryMapper.selectOrderDeliveryById(delivery.getId());
            int nowStatus = Integer.parseInt( orderDelivery.getDeliveryStatus());
            int deliveryStatus = Integer.parseInt(delivery.getDeliveryStatus());
            if(nowStatus != (deliveryStatus-1) ){
                throw new RuntimeException("订单状态错误");
            }
        }
        //状态是2  就是有人接单了 需要设置骑手的信息
        if("2".equals(delivery.getDeliveryStatus())){
            OrderDelivery orderDelivery = orderDeliveryMapper.selectOrderDeliveryById(delivery.getId());
            orderDelivery.setRiderId(delivery.getRiderId());
            // 接单时间
            orderDelivery.setAcceptTime(new Date());
            // 来源：1.抢单 2.平台派单
            orderDelivery.setSource(1);
            orderDeliveryMapper.updateOrderDelivery(orderDelivery);
        }
        orderInfoService.changeOrderStatusByOrderId(delivery);
        // 订单完成后要把冻结的金额变为待结算金额
        if(OrderStatusEnum.SUCCESS.code().equals(delivery.getDeliveryStatus())){
            walletLogService.unfreezingAmount(delivery.getOrderId());
        }
        return orderDeliveryMapper.changeStatusByOrderId(delivery.getOrderId(),delivery.getDeliveryStatus());
    }

    @Override
    public List<Long> selectUnHavePlatformOrderRiderIdList(List<Long> riderIdList) {
        List<Long> idList = orderDeliveryMapper.selectUnHavePlatformOrderRiderIdList(riderIdList);
        return idList;
    }


    /**
     * 定时查询数据库, 对已经配送完成用户没有点击确认的订单修改魏完成状态
     */
    public void changeStatusSchedule(){
        System.out.println("=============================================查询未签收订单===================================================");
        List<OrderDelivery> orderDeliverys = orderDeliveryMapper.selectListAfterDelivery(OrderStatusEnum.WAIT_TAKE_DEV.code(),OrderConstants.ORDER_COMFIRM_DAYS);
        for(OrderDelivery orderDelivery: orderDeliverys){
            orderDelivery.setDeliveryStatus(OrderStatusEnum.SUCCESS.code());
            this.changeStatusAndOrderIfoStatus(orderDelivery);
        }
    }
}
